<template>
  <div class="employess-container">
    <div class="app-container">
      <!-- 顶部 -->
      <page-tools>
        <!-- 插入到left插槽位置 -->
        <template #left>
          <span>总记录条数：{{ total }}条</span>
        </template>
        <!-- 插入到right插槽位置 -->
        <template #right>
          <el-button type="warning" size="small" @click="$router.push('/uploadExcel')">导入excel</el-button>
          <el-button type="danger" size="small" @click="exportExcel">导出excel</el-button>
          <el-button type="primary" size="small" @click="addEmployee">新增员工</el-button>
        </template>
      </page-tools>

      <el-card style="margin-top: 15px">
        <el-table border :data="list" :default-sort="{prop: 'workNumber'}">
          <el-table-column
            label="序号"
            type="index"
            :index="indexMethod"
            width="80"
          />
          <el-table-column label="姓名" prop="username" />
          <el-table-column label="头像" prop="staffPhoto">
            <template #default="{ row }">
              <el-image
                style="width: 100px; height: 100px"
                :src="row.staffPhoto"
              >
                <template>
                  <div slot="placeholder" class="image-slot">
                    加载中<span class="dot">...</span>
                  </div>
                  <div slot="error" class="image-slot">
                    <i class="el-icon-picture-outline" />
                  </div>
                </template>
              </el-image>
            </template>
          </el-table-column>
          <el-table-column label="工号" prop="workNumber" />
          <el-table-column label="聘用形式" prop="formOfEmployment">
            <template #default="{ row }">
              {{ formatEmployment(row.formOfEmployment) }}
            </template>
          </el-table-column>
          <el-table-column label="部门" prop="departmentName" />
          <el-table-column label="入职时间" sortable prop="timeOfEntry">
            <template #default="{ row }">
              {{ formatDate(row.timeOfEntry) }}
            </template>
          </el-table-column>
          <el-table-column label="账户状态" prop="enableState" />
          <el-table-column label="操作" fixed="right" width="200">
            <template #default="{ row }">
              <el-button type="text" size="small" @click="goDetail(row.id)">查看</el-button>
              <el-button type="text" size="small" @click="assignRole(row.id)">分配角色</el-button>
              <el-button type="text" size="small" @click="delEmploy(row.id)">删除</el-button>
            </template>
          </el-table-column>
        </el-table>
        <!-- 分页组件 -->
        <div style="height: 60px; margin-top: 10px">
          <el-pagination
            layout="prev, pager, next"
            :total="total"
            @current-change="pageChange"
          />
        </div>
      </el-card>
    </div>
    <!-- 弹框 -->
    <addEmployee
      :show-dialog="showDialog"
      @close-dialog="closeDialog"
      @update-list="hGetEmployeeList"
    />
    <!-- 分配角色弹框 -->
    <assignRole :show-role-dialog="showRoleDialog" :cur-id="curId " @close-roleDialog="closeRoleDialog" />
  </div>
</template>

<script>
import { getEmployeeList, delEmployee } from '@/api/employee'
import { getExportData } from '@/utils/excelData'
import dayjs from 'dayjs'
import addEmployee from './components/add-employee.vue'
import assignRole from '@/views/employees/components/assign-role.vue'
export default {
  name: 'Employess',
  components: {
    addEmployee,
    assignRole
  },
  data() {
    return {
      list: [],
      params: {
        page: 1,
        size: 10
      },
      total: 0,
      showDialog: false,
      showRoleDialog: false,
      curId: null // 当前给谁分配角色
    }
  },
  mounted() {
    this.hGetEmployeeList()
  },
  methods: {
    // 获取列表并渲染页面
    async hGetEmployeeList() {
      const res = await getEmployeeList(this.params)
      // console.log(res)
      const { rows, total } = res.data
      this.list = rows
      this.total = total
    },
    // 聘用形式
    formatEmployment(type) {
      const map = {
        1: '正式',
        2: '非正式'
      }
      return map[type]
    },
    // 分页
    pageChange(page) {
      this.params.page = page
      this.hGetEmployeeList()
    },
    // 序号连起来
    indexMethod(index) {
      return index + 1
    },
    // 时间格式化
    formatDate(date, str = 'YYYY-MM-DD') {
      return dayjs(date).format(str)
    },
    // 删除
    delEmploy(id) {
      this.$confirm('是否删除?', '提示', {
        confirmButtonText: '确定',
        cancelButtonText: '取消',
        type: 'warning'
      })
        .then(async() => {
          // 调用删除接口
          await delEmployee(id)
          // 重新获取列表
          this.hGetEmployeeList()
          this.$message({
            type: 'success',
            message: '删除成功!'
          })
        })
        .catch(() => {
          this.$message({
            type: 'info',
            message: '已取消删除'
          })
        })
    },
    // 新增员工
    addEmployee() {
      this.showDialog = true
    },
    // 关闭弹窗
    closeDialog() {
      this.showDialog = false
    },
    // 导出
    exportExcel() {
      import('@/vendor/Export2Excel').then(async(excel) => {
        console.log(excel)
        // 1.准备表头中的中英文对应关系
        const headerRelation = {
          '手机号': 'mobile',
          '姓名': 'username',
          '入职日期': 'timeOfEntry',
          '工号': 'workNumber',
          '转正日期': 'correctionTime',
          '部门': 'departmentName'
        }
        // 获取列表
        const res = await getEmployeeList(this.params)
        // 导出时的表头数据和表格数据
        const { data, header } = getExportData(res.data.rows, headerRelation)
        // excel表示导入的模块对象
        excel.export_json_to_excel({
          header: header, // 表头 必填
          data: data, // 具体数据 必填
          filename: 'excel-list', // 文件名称
          autoWidth: true, // 宽度是否自适应
          bookType: 'xlsx' // 生成的文件类型
        })
      })
    },
    // 查看
    goDetail(id) {
      this.$router.push({
        path: '/employees/employeesDetail',
        query: {
          id
        }
      })
    },
    // 分配角色
    assignRole(curId) {
      this.showRoleDialog = true
      this.curId = curId
    },
    closeRoleDialog() {
      this.showRoleDialog = false
    }
  }
}
</script>

<style>
</style>
